﻿using System;

namespace wsacq.com.util
{
    internal class UtilLog
    {
        public static void LogLine()
        {
            Log(null);
        }
        public static void Log(String msg)
        {
            // form 程序调试窗口打印
            UtilWinformTool.Log(msg);
            // 控制台打印
            Console.WriteLine(msg);
        }
        public static void WriteLog(Exception ex, string LogAddress = "")
        {
            //如果日志文件为空，则默认在Debug目录下新建 YYYY-mm-dd_Log.log文件
            if (LogAddress == "")
            {
                LogAddress = Environment.CurrentDirectory + "\\logs\\" +
                    DateTime.Now.Year + '-' +
                    DateTime.Now.Month + '-' +
                    DateTime.Now.Day + "_Log.log";
            }

            //把异常信息输出到文件
            StreamWriter sw = new StreamWriter(LogAddress, true);
            sw.WriteLine("当前时间：" + DateTime.Now.ToString());
            sw.WriteLine("异常信息：" + ex.Message);
            sw.WriteLine("异常对象：" + ex.Source);
            sw.WriteLine("调用堆栈：\n" + ex.StackTrace.Trim());
            sw.WriteLine("触发方法：" + ex.TargetSite);
            sw.WriteLine();
            sw.Close();
        }
    }

    internal class UtilWinformTool
    {
        internal static void Log(string msg)
        {
        }
    }
}
